﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Model;

namespace BO
{
    /// <summary>
    /// Summary description for VYC13BO
    /// </summary>
    public class VYC13BO:DBConnectionBO
    {
        public VYC13BO():base()
        {
            //
            // TODO: Add constructor logic here
            //
        }
        /// <summary>
        /// Lay danh sach cac du lieu trong VYC13
        /// </summary>
        /// <returns></returns>
        public List<ModelVYC13> GetAllList()
        {
            var list = new List<ModelVYC13>();
            const string query = "SELECT [Id],[LoaiLienHe],[Ten],[Email],[TieuDe],[NoiDung],[NgayCapNhat],[TrangThai] FROM [VYC-13]";
            var sqlDataAdapter = new SqlDataAdapter(query, connection);
            var dataTable = new DataTable();
            sqlDataAdapter.Fill(dataTable); // fill data to Datatable
            CloseConnection(); // Close connection to database
            for (var i = 0; i < dataTable.Rows.Count; i++)
            {
                var model = new ModelVYC13();

                model.Id = (dataTable.Rows[i][0] == DBNull.Value) ? 0 : Convert.ToInt32(dataTable.Rows[i][0]);
                model.LoaiLienHe = (dataTable.Rows[i][1] == DBNull.Value) ? string.Empty : dataTable.Rows[i][1].ToString();
                model.Ten = (dataTable.Rows[i][2] == DBNull.Value) ? string.Empty : dataTable.Rows[i][2].ToString();
                model.Email = (dataTable.Rows[i][3] == DBNull.Value) ? string.Empty : dataTable.Rows[i][3].ToString();
                model.TieuDe = (dataTable.Rows[i][4] == DBNull.Value) ? string.Empty : dataTable.Rows[i][4].ToString();
                model.NoiDung = (dataTable.Rows[i][5] == DBNull.Value) ? string.Empty : dataTable.Rows[i][5].ToString();
                model.NgayCapNhat = (dataTable.Rows[i][6] == DBNull.Value) ? DateTime.Now : (DateTime)dataTable.Rows[i][6];
                model.TrangThai = (dataTable.Rows[i][7] == DBNull.Value) ? 0 : Convert.ToInt32(dataTable.Rows[i][7]);

                list.Add(model);
            }
            return list;
        }

        /// <summary>
        /// Add new a model in data
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int AddData(ModelVYC13 model)
        {
            var query = "INSERT INTO [VYC-13]([LoaiLienHe],[Ten],[Email],[TieuDe],[NoiDung],[NgayCapNhat],[TrangThai]) VALUES (" +
                        "'" + model.LoaiLienHe + "', N'" 
                        + model.Ten + "', '"
                        + model.Email + "', N'"
                        + model.TieuDe + "', N'" + model.NoiDung + "', "
                        + "GETDATE(), " + model.TrangThai + ")";
            var sqlCommand = new SqlCommand(query, connection);
            try
            {
                var status = sqlCommand.ExecuteNonQuery();
                CloseConnection(); // close connection
                return status;
            }
            catch (Exception)
            {
                CloseConnection();
                return 0;
                throw;
            }
        }

        /// <summary>
        /// Edit a model in data
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int EditData(ModelVYC13 model)
        {
            var query = "UPDATE [VYC-13] SET " +
                        "LoaiLienHe= '" + model.LoaiLienHe + "'," +
                        "Ten = N'" + model.Ten + "', " +
                        "Email= '" + model.Email + "', " +
                        "TieuDe = N'" + model.TieuDe + "'," +
                        "NoiDung = N'" + model.NoiDung + "'," +
                        "NgayCapNhat = GETDATE(), TrangThai = " + model.TrangThai +
                        " WHERE Id = " + model.Id;
            var sqlCommand = new SqlCommand(query, connection);
            try
            {
                var status = sqlCommand.ExecuteNonQuery();
                CloseConnection(); // close connection
                return status;
            }
            catch (Exception)
            {
                CloseConnection();
                return 0;
                throw;
            }
        }

        /// <summary>
        /// Delete a model in data
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int DeleteData(ModelVYC13 model)
        {
            var query = "DELETE FROM [VYC-13] " +
                        " WHERE Id = " + model.Id;
            var sqlCommand = new SqlCommand(query, connection);
            try
            {
                var status = sqlCommand.ExecuteNonQuery();
                CloseConnection(); // close connection
                return status;
            }
            catch (Exception)
            {
                CloseConnection();
                return 0;
                throw;
            }
        }

        /// <summary>
        /// Get by id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public List<ModelVYC13> GetById(int id)
        {
            var list = new List<ModelVYC13>();
            var query = "SELECT [Id],[LoaiLienHe],[Ten],[Email],[TieuDe],[NoiDung],[NgayCapNhat],[TrangThai] FROM [VYC-13] WHERE Id=" + id;
            var sqlDataAdapter = new SqlDataAdapter(query, connection);
            var dataTable = new DataTable();
            sqlDataAdapter.Fill(dataTable); // fill data to Datatable
            CloseConnection(); // Close connection to database
            for (var i = 0; i < dataTable.Rows.Count; i++)
            {
                var model = new ModelVYC13();

                model.Id = (dataTable.Rows[i][0] == DBNull.Value) ? 0 : Convert.ToInt32(dataTable.Rows[i][0]);
                model.LoaiLienHe = (dataTable.Rows[i][1] == DBNull.Value) ? string.Empty : dataTable.Rows[i][1].ToString();
                model.Ten = (dataTable.Rows[i][2] == DBNull.Value) ? string.Empty : dataTable.Rows[i][2].ToString();
                model.Email = (dataTable.Rows[i][3] == DBNull.Value) ? string.Empty : dataTable.Rows[i][3].ToString();
                model.TieuDe = (dataTable.Rows[i][4] == DBNull.Value) ? string.Empty : dataTable.Rows[i][4].ToString();
                model.NoiDung = (dataTable.Rows[i][5] == DBNull.Value) ? string.Empty : dataTable.Rows[i][5].ToString();
                model.NgayCapNhat = (dataTable.Rows[i][6] == DBNull.Value) ? DateTime.Now : (DateTime)dataTable.Rows[i][6];
                model.TrangThai = (dataTable.Rows[i][7] == DBNull.Value) ? 0 : Convert.ToInt32(dataTable.Rows[i][7]);

                list.Add(model);
            }
            return list;
        }
    }
}